<?php
/**
 * 微信小程序接口类
 *  app id    wx51fa2f9eabf66605
 *  secert:   1d3e10ce3ea7da269e10f7805564d2c9
 */

namespace lib\wechat;

class WxSmallClient {
    private static $WX_URL    = 'https://api.weixin.qq.com';
    // 麦当劳的
    private static $WX_APP_ID = 'wx6d2ebac5a61ba4e1';
    private static $WX_SECRET = '66eca1bd3490bef2a9dd6d6b8f53774b';

    static public  function getSessionKey($code)
    {
        if (empty($code)) {
            return false;
        }
        $data = [
            'appid'      => self::$WX_APP_ID,
            'secret'     => self::$WX_SECRET,
            'js_code'    => $code,
            'grant_type' => 'authorization_code'
        ];
        $urls = self::$WX_URL . '/sns/jscode2session?appid='.self::$WX_APP_ID.'&secret='.self::$WX_SECRET.'&js_code='.$code.'&grant_type=authorization_code';
        //var_dump($urls);exit;
        return curl($urls);
    }

    /**
     * 根据session_key解密用户数据
     */
    public static function decryptData($session_key, $iv, $datas)
    {
        include_once "bizcrypt/WxBizDataCrypt.php";
        $pc = new \WXBizDataCrypt(self::$WX_APP_ID, $session_key);
        $rs = $pc->decryptData( $datas, $iv, $data );
        if ($rs == 0) {
            return $data;
        } else {
            return $rs;
        }
    }
}